package com.zzyl.nursing.controller.member;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationAddDTO;
import com.zzyl.nursing.dto.ReservationCountDTO;
import com.zzyl.nursing.dto.ReservationPageDTO;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequestMapping("/member/reservation")
public class ReservationMemberController extends BaseController {


    @Autowired
    private TokenService tokenService;
    @Autowired
    private IReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("取消预约")
    public R<Integer> getCancelledBookingCount() {
        Long userId  = UserThreadLocal.getUserId();
        if (userId == null) {
            return R.fail(401, "无法查询取消预约");
        }
        System.out.println("查询用户手机号：" + userId);
        LocalDate today = LocalDate.now();
        //开始时间
        LocalDateTime startTime = today.atStartOfDay();
        //结束时间
        LocalDateTime endTime = today.atTime(LocalTime.MAX);
        // 查询次数
        int cancelledCount = reservationService.todayCancelledCountByUserId(
                startTime,
                endTime
        );
        return R.ok(cancelledCount);
    }
    @GetMapping("/countByTime")
    @ApiOperation("查询剩余预约次数")
    public AjaxResult getByTime(@RequestParam Long time) {


        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return AjaxResult.error(401, "无法查询预约次数");
        }
        List<ReservationCountDTO> countList = reservationService.remainingCountOnDate(time);
        return AjaxResult.success(countList);
    }
    @PostMapping()
    @ApiOperation("新增预约")
    public AjaxResult createBooking(@Valid @RequestBody ReservationAddDTO addDTO) {
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return AjaxResult.error(401, "无法新增预约");
        }
        reservationService.AddSchedule(addDTO,userId);
        return AjaxResult.success("成功");
    }
    @GetMapping("/page")
    public AjaxResult selectBooking(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Integer status) {

        // 验证登录状态
        Long userId = UserThreadLocal.getUserId();
        if (userId == null) {
            return AjaxResult.error(401, "无法查询");
        }

        //  分页查询数据
        Page<Reservation> page = new Page<>(pageNum, pageSize);
        IPage<Reservation> pageResult = reservationService.selectBookingPage(page,userId,status);
        ReservationPageDTO innerData = new ReservationPageDTO();
        innerData.setTotal(pageResult.getTotal());
        innerData.setRows(pageResult.getRecords());
        return AjaxResult.success(innerData)
                .put("code", 200)
                .put("msg", "成功");
    }
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelBooking(@PathVariable("id") Long id) {
        //校验参数
        if (id == null || id <= 0) {
            return AjaxResult.error("预约ID不能为空");
        }

        boolean success = reservationService.cancelBooking(id);

        if (success) {
            return AjaxResult.success("取消成功");
        }
        return AjaxResult.error("取消失败，请检查预约状态");
    }
    //    @GetMapping("/page")
//    public String page(){
//        // 获取当前登录人
//        Long userId = UserThreadLocal.get();
//        System.out.println(userId);
//        return "success";
//    }
}

